package com.im.controller;

import com.im.MDA;
import com.im.service.dto.BaseException;
import com.im.service.dto.FixAsset;
import com.im.service.dto.PageQuery;
import com.im.service.dto.Staff;
import com.im.service.fixAsset.smo.IFixAssetOperSmo;
import com.im.service.fixAsset.smo.IFixAssetQuerySmo;
import com.im.util.JsonUtil;
import com.im.util.ReturnsUtil;
import com.im.viewmodel.fixAsset.FixAssetRspVo;
import com.im.viewmodel.fixAsset.YearDtListRspVo;
import net.sf.json.JSONObject;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

/**
 * 固定资产后端服务.
 * @author wangxh
 *
 */
@RestController
@RequestMapping("/fixAssetService")
public class FixAssetService {

    private static final Logger logger = LogManager.getLogger(FixAssetService.class);

    @Autowired
    private IFixAssetOperSmo fixAssetOperSmo;
    @Autowired
    private IFixAssetQuerySmo fixAssetQuerySmo;

    /**
     * 固定资产列表查询
     */
    @RequestMapping(value = "/queryFixAssetListForUI", method = RequestMethod.POST)
    @ResponseBody
    public String queryFixAssetListForUI(@RequestBody String inJson) {
        if (logger.isInfoEnabled()) {
            logger.info("查询固定资产列表请求入参json:{}", inJson);
        }
        if (inJson == null || inJson.length() <= 0) {
            throw new BaseException("请求json入参为空，请核查数据!");
        }
        try {
            List<String> typelist = new ArrayList<String>();
            typelist.add("10");
            typelist.add("20");
            PageQuery<FixAssetRspVo> fixAssetRspVo = fixAssetQuerySmo.queryFixAssetListForUI(inJson,typelist);
            String result = ReturnsUtil.returnSuccess(fixAssetRspVo).toString();
            return result;
        } catch (Exception e) {
            logger.error(e.getMessage());
            return ReturnsUtil.returnException("查询固定资产列表失败！", e).toString();
        }

    }

    /**
     * 固定资产年份列表查询
     */
    @RequestMapping(value = "/queryFixAssetYearDtList", method = RequestMethod.POST)
    @ResponseBody
    public String queryFixAssetYearDtList(@RequestBody String inJson) {
        try {
            List<String> typelist = new ArrayList<String>();
            YearDtListRspVo yearDtListRspVo = fixAssetQuerySmo.queryFixAssetYearDtList(inJson,typelist);
            String result = ReturnsUtil.returnSuccess(yearDtListRspVo).toString();
            return result;
        } catch (Exception e) {
            logger.error(e.getMessage());
            return ReturnsUtil.returnException("查询固定资产列表失败！", e).toString();
        }

    }


    /**
     *
     * 新增固定资产申报设置
     *
     * @param jsonStr
     *            入参
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/addFixAsset", method = RequestMethod.POST)
    public String addFixAsset(HttpServletRequest request, @RequestBody String jsonStr) throws Exception {
        if (logger.isInfoEnabled()) {
            logger.info("新增固定资产申报设置请求入参json:{}", jsonStr);
        }
        try {
            if (jsonStr == null || jsonStr.length() <= 0) {
                throw new BaseException("新增固定资产申报设置请求json入参为空，请核查数据!");
            }
            HttpSession session = request.getSession();
            Staff staff = (Staff) session.getAttribute("loginedStaff");
            String result = fixAssetOperSmo.addFixAsset(jsonStr, staff);
            JSONObject jsonObj = JSONObject.fromObject(result);
            String resultCode = JsonUtil.getStringFromJSON(jsonObj, "resultCode", "");
            if (MDA.RESULT_SUCCESS.equals(resultCode)) {
                return result;
            } else {
                return ReturnsUtil.returnFailure(JsonUtil.obtainValueFromJsonObject(result, "message"));
            }
        } catch (BaseException e) {
            if (logger.isErrorEnabled()) {
                logger.error(e.getStackTrace());
            }
            return ReturnsUtil.returnFailure(e.getMessage());
        } catch (Exception e) {
            if (logger.isErrorEnabled()) {
                logger.error(ReturnsUtil.getStackTrackMessage(e));
            }
            return ReturnsUtil.returnException("新增投资计划申报设置失败", e).toString();
        }
    }

    /**
     *
     * 删除固定资产
     *
     * @param jsonStr
     *            入参
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/updateFixAsset", method = RequestMethod.POST)
    public String updateFixAsset(HttpServletRequest request, @RequestBody String jsonStr) throws Exception {
        if (logger.isInfoEnabled()) {
            logger.info("删除固定资产请求入参json:{}", jsonStr);
        }
        try {
            if (jsonStr == null || jsonStr.length() <= 0) {
                throw new BaseException("删除固定资产请求json入参为空，请核查数据!");
            }
            HttpSession session = request.getSession();
            Staff staff = (Staff) session.getAttribute("loginedStaff");
            String result = fixAssetOperSmo.updateFixAsset(jsonStr, staff);
            JSONObject jsonObj = JSONObject.fromObject(result);
            String resultCode = JsonUtil.getStringFromJSON(jsonObj, "resultCode", "");
            if (MDA.RESULT_SUCCESS.equals(resultCode)) {
                return result;
            } else {
                return ReturnsUtil.returnFailure(JsonUtil.obtainValueFromJsonObject(result, "message"));
            }
        } catch (BaseException e) {
            if (logger.isErrorEnabled()) {
                logger.error(e.getStackTrace());
            }
            return ReturnsUtil.returnFailure(e.getMessage());
        } catch (Exception e) {
            if (logger.isErrorEnabled()) {
                logger.error(ReturnsUtil.getStackTrackMessage(e));
            }
            return ReturnsUtil.returnException("删除固定资产失败", e).toString();
        }
    }

    /**
     *
     * 固定资产审定
     *
     * @param jsonStr
     *            入参
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/updateFixAssetStatus", method = RequestMethod.POST)
    public String updateFixAssetStatus(HttpServletRequest request, @RequestBody String jsonStr) throws Exception {
        if (logger.isInfoEnabled()) {
            logger.info("固定资产审定请求入参json:{}", jsonStr);
        }
        try {
            if (jsonStr == null || jsonStr.length() <= 0) {
                throw new BaseException("固定资产审定请求json入参为空，请核查数据!");
            }
            HttpSession session = request.getSession();
            Staff staff = (Staff) session.getAttribute("loginedStaff");
            String result = fixAssetOperSmo.updateFixAssetStatus(jsonStr, staff);
            JSONObject jsonObj = JSONObject.fromObject(result);
            String resultCode = JsonUtil.getStringFromJSON(jsonObj, "resultCode", "");
            if (MDA.RESULT_SUCCESS.equals(resultCode)) {
                return result;
            } else {
                return ReturnsUtil.returnFailure(JsonUtil.obtainValueFromJsonObject(result, "message"));
            }
        } catch (BaseException e) {
            if (logger.isErrorEnabled()) {
                logger.error(e.getStackTrace());
            }
            return ReturnsUtil.returnFailure(e.getMessage());
        } catch (Exception e) {
            if (logger.isErrorEnabled()) {
                logger.error(ReturnsUtil.getStackTrackMessage(e));
            }
            return ReturnsUtil.returnException("删除固定资产失败", e).toString();
        }
    }

}
